package com.LFLWZ.controller;
import com.LFLWZ.beans.Dto.Dto;
import com.LFLWZ.beans.POJO.ConcernLecturer;
import com.LFLWZ.beans.POJO.Lecturer;
import com.LFLWZ.beans.Vo.LerAndCrsVo;
import com.LFLWZ.beans.Vo.addLecturer;
import com.LFLWZ.service.LecturerService;
import com.LFLWZ.utils.DtoUtil;
import com.LFLWZ.utils.EmptyUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Lecturer)表控制层
 *
 * @author 淋漓
 * @since 2020-12-04 01:31:06
 */
@RestController
@RequestMapping("lecturer")
public class LecturerController {
    /**
     * 服务对象
     */
    @Reference
    private LecturerService lecturerService;

    @GetMapping("selectByUid/{uid}")
    public Dto selectByUid(@PathVariable Integer uid){
        Dto dto=null;
        if (uid==null) {
            dto = DtoUtil.returnError(4001, "参数错误");
            return dto;
        }
        Lecturer lecturer=new Lecturer();
        lecturer.setUid(uid);
        Lecturer param= lecturerService.select(lecturer);
        if(param!=null){
            dto=DtoUtil.returnSuccessData(param);
        }else {
            dto=DtoUtil.returnError(4002,"查询失败");
        }
        return dto;
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Lecturer selectOne(Integer id) {
        return null;
    }

    /***
     * 查询全部讲师表(现是测试后续会改)
     * @return
     */
    @GetMapping("selall")
    public Dto selall() {
        List list = lecturerService.queryAll();
        if (list == null) {
            return DtoUtil.returnError(4001, "程序错误,查询失败");
        } else {
            return DtoUtil.returnSuccessData(list);
        }
    }

    /***
     * 根据ID 查询讲师详情
     * @param id 讲师ID
     * @return 讲师数据
     */
    @GetMapping("selallById/{id}")
    public Dto selallById(@PathVariable Integer id) {
        Dto dto = null;
        if (id == null) {
            id = 0;
        }
        List<Lecturer> list = lecturerService.queryById(id);
        if (EmptyUtils.isEmpty(list)) {
            dto = DtoUtil.returnError(4001, "没有该数据");
        } else {
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }

    /***
     * 根据讲师ID 查询各讲师课程
     * @param id    讲师ID
     * @return 课程数据
     */
    @GetMapping("selcourse/{id}")
    public Dto selcourse(@PathVariable Integer id) {
        Dto dto = null;
        List<LerAndCrsVo> list = lecturerService.selcourse(id);
        if (EmptyUtils.isEmpty(list)) {
            dto = DtoUtil.returnError(4001, "没有该课程");
        } else {
            dto = DtoUtil.returnSuccessData(list);
        }
        return dto;
    }

    /***
     *  根据讲师ID 查询各讲师课程总数
     * @param id    讲师ID
     * @return 课程总数
     */
    @GetMapping("selCountCre/{id}")
    public Dto selCountCre(@PathVariable Integer id) {
        Dto dto = null;
        int Count = lecturerService.selCountCre(id);
        if (EmptyUtils.isEmpty(Count)) {
            dto = DtoUtil.returnError(4001, "老师没有课程");
        } else {
            dto = DtoUtil.returnSuccessData(Count);
        }
        return dto;
    }

    /***
     * 查询优秀讲师
     * @return 优秀讲师 数据
     */
    @GetMapping("selExc")
    public Dto selExc() {
        Dto dto = null;
        List<Lecturer> list = lecturerService.selExc();
        if (EmptyUtils.isEmpty(list)) {
            dto = DtoUtil.returnError(4001, "程序错误,没有优秀讲师");
        } else {
            dto = DtoUtil.returnSuccessData(list);
        }
        return DtoUtil.returnSuccessData(lecturerService.selExc());
    }

    /***
     * 关注讲师
     * @param
     * @return 200:新增关注 100:取消关注
     */
    @PostMapping("ins")
    public Dto ins(@RequestBody addLecturer addLecturer){
        int line = 0;
        ConcernLecturer concernLecturer = new ConcernLecturer();
        concernLecturer.setUid(addLecturer.getUid());
        concernLecturer.setLecturerId(addLecturer.getLecturerId());
        List<ConcernLecturer> list = lecturerService.seladd(concernLecturer);
        if(EmptyUtils.isEmpty(list)){
            line = lecturerService.ins(addLecturer);
            return DtoUtil.returnError(200,"关注成功");
        }else{
            line = lecturerService.del(concernLecturer);
            return DtoUtil.returnError(100, "取消关注");
        }
    }

    /***
     * 查询 关注的讲师!!!!!
     * @param uid   根据uid查询
     * @return 4001:参数错误 成功返回list
     */
    @GetMapping("selLike/{uid}")
    public Dto selLike(@PathVariable  Integer uid){
        Dto dto = null;
        ConcernLecturer concernLecturer =  new ConcernLecturer();
        concernLecturer.setUid(uid);
        List<ConcernLecturer> list  = lecturerService.seladd(concernLecturer);
        if(EmptyUtils.isEmpty(list)){
            return DtoUtil.returnError(4001,"参数错误");
        }else{
            return DtoUtil.returnSuccessData(list);
        }
    }
    @GetMapping("selByRoom/{id}")
    public Dto selByRoom(@PathVariable Integer id){
        Dto dto=null;
        if (id==null)
            dto=DtoUtil.returnError(4001,"参数错误");
        System.out.println(id);
        Lecturer lecturer= lecturerService.selByRoom(id);
//        if (lecturer!=null){
        dto=DtoUtil.returnSuccessData(lecturer);
//        System.out.println(lecturer.getUid());
//        }else {
//            dto=DtoUtil.returnError(4002,"查询为空");
//        }
        return dto;
    }
    @GetMapping("selCount/{lecturerId}")
    public Dto selCount(@PathVariable  Integer lecturerId){
        int line = lecturerService.selCount(lecturerId);
        if(line>0){
            return DtoUtil.returnSuccessData(line);
        }else {
            return DtoUtil.returnError(4001, "查询失败");
        }
    }
}